package com.manor.paradise.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import com.manor.paradise.R;

import androidx.annotation.Nullable;

public class DrawView extends View {
    private int mTotalProgress = 100;
    private int mProgress = 0;

    private Paint mTextPaint;

    private Paint mPaint;

    private RectF mRectF;

    private int mTextColor = Color.WHITE;
    private Paint paint;

    public DrawView(Context context) {
        super(context);
        initView();
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        paint = new Paint();
        mPaint = new Paint();
        mTextPaint = new Paint();
    }

    public void setmProgress(int progress) {
        if (progress > 100) {
            mProgress = 100;
        } else {
            mProgress = progress;
        }
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRectF = new RectF(20, 20, w - 20, h - 20);
        int colorResA = getResources().getColor(R.color.colorRed_0);
        int colorResB = getResources().getColor(R.color.colorRed_1);
        int colorResC = getResources().getColor(R.color.colorRed_2);
        int colorResD = getResources().getColor(R.color.colorRed_3);
//        int colorResE = getResources().getColor(R.color.colorRed_4);
//        int colorResF = getResources().getColor(R.color.colorRed_0);

        int[] arrayList = new int[]{colorResA, colorResB, colorResC, colorResD};
        SweepGradient sweepGradient = new SweepGradient(mRectF.centerX(), mRectF.centerY(), arrayList, null);
        mPaint.setShader(sweepGradient);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mProgress < 100) {
            paint.setAntiAlias(true);
            paint.setColor(Color.parseColor("#FF484949"));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(8);
            canvas.drawArc(mRectF, 360 * mProgress / mTotalProgress - 90, 359 - (360 * mProgress / mTotalProgress),
                    false, paint);
        }
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(36);
        mPaint.setPathEffect(new DashPathEffect(new float[]{5, 5}, 0));

        canvas.drawArc(mRectF, -90, 360 * mProgress / mTotalProgress, false, mPaint);


        mTextPaint.setColor(mTextColor);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setStrokeWidth(36);
        mTextPaint.setTextSize(getWidth() / 6);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        int mTextSize = (int) mTextPaint.getTextSize();
        canvas.drawText(String.valueOf(mProgress) + "%", getWidth() / 2, getHeight() / 2 + mTextSize / 4, mTextPaint);


    }
}
